const { Forum } = require('../models/forum.js')
const { ForumContent } = require('../models/forum_content.js')
const { ForumReply } = require('../models/forum_reply.js')
const sendMsg = require('../utils').SendMsg
const getUserIp = require('../utils/getUserIp.js').GetUserIp
const moment = require('moment');

/**
 * 	添加一条贴主题
 *  forumid: 'String',
 * 	content: 'String'
 */
exports.addForum = async (ctx, next) => {
	let contentss = ctx.request.body.content
	let simplecontent = contentss.length > 60 ? contentss.substring(0, 60) + '...' : contentss
	let query = {
		userid: ctx.request.body.usertoken,
		titles: ctx.request.body.titles,
		ip: getUserIp(ctx.request),
		createtime: moment().format('YYYY-MM-DD HH:mm:ss'),
		repleycount: ctx.request.body.repleycount,
		replyuserid: ctx.request.body.replyuserid,
		lastreplytime: moment().format('YYYY-MM-DD HH:mm:ss'),
		simplecontent: simplecontent,
		favor: ctx.request.body.favor
	}
	let data = await Forum.create(query)
	if (!data) {

	} else { // 提交内容到content
		let _data = data
		let nowcontent = {
			forumid: data._id,
			content: ctx.request.body.content
		}
		let contentData = await ForumContent.create(nowcontent)
		if (!contentData) {

		} else {
			ctx.body = {
				status: 0,
				result: {
					forum: _data,
					forum_content: contentData
				}
			}
		}
	}
}

/**
 * 给一条主题帖回复
 *  forumid: 'String',
 *  userid: 'String',
 *  content: 'String',
 *  replyTime: 'String',
 *  ip: 'String'
 */
exports.replyForum = async (ctx, next) => {
	let query = {
		forumid: ctx.request.body.forumid,
		userid: ctx.request.body.userid,
		content:  ctx.request.body.content,
		replyTime: moment().format('YYYY-MM-DD HH:mm:ss'),
		ip: getUserIp(ctx.request)
	}

	let data = await ForumReply.create(query)
	if (!data) {

	} else {
		ctx.body = {
			status: 0,
			result: data
		}
	}
}

/**
 * 获取所有主题帖
 */
exports.getForum = async (ctx, next) => {
	let query = {
		page: Number(ctx.request.body.page),
		pageNum: Number(ctx.request.body.pageNum) ||　10
	}
	let data = await Forum.find().skip((query.page - 1) * query.pageNum).limit(query.pageNum)
	if (!data) {

	} else {
		ctx.body = {
			status: 0,
			result: data
		}
	}
}

/**
 * 获取单个贴详细内容
 */
exports.getForumContent = async (ctx, next) => {
	let query = {
		forumid: ctx.request.body.forumid
	}
	let data = await ForumContent.find({forumid: query.forumid})
	if (!data) {

	} else {
		ctx.body = {
			status: 0,
			result: data
		}
	}
}

/**
 * 获取单个贴下所有的回复
 */
exports.getForumReply = async (ctx, next) => {
	let query = {
		forumid: ctx.request.body.forumid,
		page: Number(ctx.request.body.page),
		pageNum: Number(ctx.request.body.pageNum) ||　10
	}
	let data = await ForumReply.find({forumid: query.forumid}).skip((query.page - 1) * query.pageNum).limit(query.pageNum)
	if (!data) {

	} else {
		ctx.body = {
			status: 0,
			result: data
		}
	}
}

/**
 * 添加想看
 * 
 */
exports.addFavor = async (ctx, next) => {
	let query = {
		forumid: ctx.request.body.forumid
	}
	let data = await Forum.update({"_id": query.forumid}, {$inc:{favor: 1}})
	if (!data) {

	} else {
		ctx.body = {
			status: 0,
			result: data
		}
	}
}
/**
 * 移除想看
 */
exports.removeFavor = async (ctx, next) => {
	let query = {
		forumid: ctx.request.body.forumid
	}
	let data = await Forum.update({"_id": query.forumid}, {$inc:{favor: -1}})
	if (!data) {

	} else {
		ctx.body = {
			status: 0,
			result: data
		}
	}
}